<?php
/**
* sharing backend for resume 
*
*/
require_once(OC_App::getAppPath('resumes') . "/lib/resume.php");

class OC_Share_Backend_Resume implements OCP\Share_Backend {
   const FORMAT_RESUME = 1;

   /**
   * @brief Get the source of the item to be stored in the database
   * @param string Item
   * @param string Owner of the item // not needed only in resumes share
   * @return mixed|array|false Source
   *
   * Return an array if the item is file dependent, the array needs two keys: 'item' and 'file'
   * Return false if the item does not exist for the user
   *
   * The formatItems() function will translate the source returned back into the item
   */
   public function isValidSource($itemSource, $uidOwner) {
      if(empty($itemSource))
         return false; 

      $user = PFactory::getMasterUser();
      if(!$user->id)
         return false;

      $resumeids = explode(",", $itemSource);

      $model = new PResumesModelResume();
      $resumes = $model->getResumeAsFiles($user->id, $resumeids);
      if (empty($resumes)) {
         return false;
      }
      return true;
   }

   /**
   * @brief Get a unique name of the item for the specified user
   * @param string Item
   * @param string|false User the item is being shared with
   * @param array|null List of similar item names already existing as shared items
   * @return string Target name
   *
   * This function needs to verify that the user does not already have an item with this name.
   * If it does generate a new name e.g. name_#
   */
   public function generateTarget($itemSource, $shareWith, $exclude = null) {
      $resumeids = explode(",", $itemSource);
      $user = PFactory::getMasterUser();
      if(!$user->id)
        return $resumes;

      $model = new PResumesModelResume();
      $resumes = $model->getResumeAsFiles($user->id, $resumeids);

      $resume  = $resumes[0]; 

      return $resume->name . "/" . $resume->id . "/" . $resume->jobtitle; 
   }

   /**
   * @brief Converts the shared item sources back into the item in the specified format
   * @param array Shared items
   * @param int Format
   * @return ?
   *
   * The items array is a 3-dimensional array with the item_source as the first key and the share id as the second key to an array with the share info.
   * The key/value pairs included in the share info depend on the function originally called:
   * If called by getItem(s)Shared: id, item_type, item, item_source, share_type, share_with, permissions, stime, file_source
   * If called by getItem(s)SharedWith: id, item_type, item, item_source, item_target, share_type, share_with, permissions, stime, file_source, file_target
   * This function allows the backend to control the output of shared items with custom formats.
   * It is only called through calls to the public getItem(s)Shared(With) functions.
   */
   public function formatItems($items, $format, $parameters = null) {
      $resumes = array();
      if ($format == self::FORMAT_RESUME) {
         $resumeids = array(); 
         foreach ($items as $item) {
            array_push($resumeids, $item['item_source']); 
         }
         JArrayHelper::toInteger($resumeids); 
        
         $user = PFactory::getMasterUser();
         $companyid = $user->id;
         if(!$companyid)
            return $resumes; 
 
         $model = new PResumesModelResume();
         $list = $model->getResumeAsFiles($companyid, $resumeids);
         
         foreach($list as $resume) {
            foreach ($items as $item) {
               if($item['item_source'] == $resume->id) {
                  $resume->displaynamename = $resume->name; 
                  $resume->resumefile = "/".$companyid."/resumes/" . $resume->id . "/resume.html"; 
                  $resume->permissions = $item->permissions;
                  $resumes[] = $resume;
                  break; 
               }
            }
         }
      }
      return $resumes; 
   }

}
